package chixing.day19;

import java.sql.SQLOutput;

public class ThreadLocalExample {
  private static ThreadLocal<Integer> balance = ThreadLocal.withInitial(() -> 0);
  public void deposit(int amount)
  {
      balance.set(balance.get() + amount);
      System.out.println(Thread.currentThread().getName() + " 存款成功，当前余额: " + balance.get());
  }
  public static void main(String[] args)
  {
      ThreadLocalExample example = new ThreadLocalExample();
      Runnable task = () -> {
          for (int i = 0; i < 3; i++)
          {
              example.deposit(100);
          }
      };
      new Thread(task, "线程1").start();
      new Thread(task, "线程2").start();
      Integer i = balance.get();
      System.out.println("main线程的balance: " + i);
  }

}
